package cli;

import tuple.data.NewDataRule;
import tuple.entity.EntityRule;
import tuple.permission.PermissionRule;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigManager {
    Properties properties = new Properties();
    public String androidJarPlatform;
    public String sourcesAndSinksPath;
    public String callbacksPath;
    public String sourcePath;
    public String sinkPath;
    public String pathToDataOnotology;
    public String pathToEntityOntology;
    public String pathToPermissionStrings;
    public String pathToPermissionMethods;
    public boolean recordSystemOutLog=true;
    public boolean recordScanLog=true;
    public boolean recordFlowLog=true;

    public int SIZE1=10;
    public int SIZE2=10;

    //     time used for dataflow, (seconds)
    public int SMALL_APK_DATAFLOW = 600;
    public int MIDDLE_APK_DATAFLOW = 1200;
    public int LARGE_APK_DATAFLOW = 1800;

    // time used for path reconstruction, each path max (seconds)
    public int SMALL_APK_PER_PATH = 60;
    public int MIDDLE_APK_PER_PATH = 75;
    public int LARGE_APK_PER_PATH = 90;

    // time used for path reconstruction, total time max (seconds)
    public int SMALL_APK_TOTAL = 1200;
    public int MIDDLE_APK_TOTAL = 1200;
    public int LARGE_APK_TOTAL = 1200;

    /**
     * 从 config.properties 里读取配置
     */
    private ConfigManager() {
        try (InputStream inputStream = ConfigManager.class.getClassLoader().getResourceAsStream("config.properties")) {
            // 读取配置文件
            properties.load(inputStream);

            // 读取配置文件
            androidJarPlatform = properties.getProperty("localAndroidJar");
            // 废弃的2个配置项 sourcesAndSinksPath   callbacksPath
            sourcesAndSinksPath = properties.getProperty("locationSrcsAndSinks");
            callbacksPath = properties.getProperty("localCallbacksPath");
            // 指向 config 下的2个文件
            sourcePath = properties.getProperty("sourcePath");
            sinkPath = properties.getProperty("sinkPath");
            // 指向 Ontology 下的几个文件
            pathToDataOnotology = properties.getProperty("pathToDataOntology");
            pathToEntityOntology = properties.getProperty("pathToEntityOntology");
            pathToPermissionStrings= properties.getProperty("pathToPermissionStrings");
            pathToPermissionMethods = properties.getProperty("pathToPermissionMethods");

            // 是否存储日志
            // 系统输出日志（用于检查进行到什么阶段了）
            recordSystemOutLog = Boolean.parseBoolean(properties.getProperty("recordSystemOutLog"));
            // 扫描阶段的结果
            recordScanLog = Boolean.parseBoolean(properties.getProperty("recordScanLog"));
            // 污点分析阶段的结果
            recordFlowLog = Boolean.parseBoolean(properties.getProperty("recordFlowLog"));

            // 内存资源和时间资源管理
            SIZE1=Integer.parseInt(properties.getProperty("SIZE1"));
            SIZE2=Integer.parseInt(properties.getProperty("SIZE2"));
            SMALL_APK_DATAFLOW=Integer.parseInt(properties.getProperty("SMALL_APK_DATAFLOW"));
            MIDDLE_APK_DATAFLOW=Integer.parseInt(properties.getProperty("MIDDLE_APK_DATAFLOW"));
            LARGE_APK_DATAFLOW=Integer.parseInt(properties.getProperty("LARGE_APK_DATAFLOW"));
            SMALL_APK_PER_PATH=Integer.parseInt(properties.getProperty("SMALL_APK_PER_PATH"));
            MIDDLE_APK_PER_PATH=Integer.parseInt(properties.getProperty("MIDDLE_APK_PER_PATH"));
            LARGE_APK_PER_PATH=Integer.parseInt(properties.getProperty("LARGE_APK_PER_PATH"));
            SMALL_APK_TOTAL=Integer.parseInt(properties.getProperty("SMALL_APK_TOTAL"));
            MIDDLE_APK_TOTAL=Integer.parseInt(properties.getProperty("MIDDLE_APK_TOTAL"));
            LARGE_APK_TOTAL=Integer.parseInt(properties.getProperty("LARGE_APK_TOTAL"));


            NewDataRule.setDataOntologyPath(pathToDataOnotology);
            EntityRule.setEntityOntologyPath(pathToEntityOntology);
            PermissionRule.setTwoPaths(pathToPermissionStrings, pathToPermissionMethods);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static ConfigManager getInstance() {
        return new ConfigManager();
    }

    public static ConfigManager getInstanceWithParams(String androidJarPlatform,
                                                      String sourcesAndSinksPath,
                                                      String callbacksPath,
                                                      String sourcePath,
                                                      String sinkPath,
                                                      String pathToDataOnotology,
                                                      String pathToEntityOntology,
                                                      String pathToPermissionStrings,
                                                      String pathToPermissionMethods) {
        ConfigManager configManager = new ConfigManager();
        configManager.androidJarPlatform = androidJarPlatform;
        configManager.sourcesAndSinksPath = sourcesAndSinksPath;
        configManager.callbacksPath = callbacksPath;
        configManager.sourcePath = sourcePath;
        configManager.sinkPath = sinkPath;
        configManager.pathToDataOnotology = pathToDataOnotology;
        configManager.pathToEntityOntology = pathToEntityOntology;
        configManager.pathToPermissionStrings = pathToPermissionStrings;
        configManager.pathToPermissionMethods = pathToPermissionMethods;
        return configManager;

    }
}
